package com.wc.算法基础课.D第四讲数学知识.扩展欧几里得算法.线性同余方程;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/22 22:57
 * @description https://www.acwing.com/problem/content/880/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int n, a, b, m;
    static int[] x = new int[1], y = new int[1];
    /***
     * 题目让我们求这个等式a * x ≡ b(mod m)中的x
     * 我们可以将这样的等式转化成《扩张欧几里得算法》来算
     * 等式的关系是 ax % m = b ,我们转化成乘法关系，ax = my + b
     * 因为ax是m的倍数加b ，所以才能ax % m = b，
     * 在变形得 ax - my = b，设y1 = -y得到ax + my1 = b
     * 就成了我们的《扩张欧几里得算法》等式
     * 然后利用《扩展欧几里得算法》的模板求出一个gcd(a,m)即设为d
     * 《扩展欧几里得算法》可以得出一对x,y使得ax + my = d成立
     * 要想让ax + my = d 右边变成b的话
     * 等式两边同时乘以b / d 得到：(b / d)(ax + my) = d * (b / d)
     * 展开得到: (b / d) ax + (b / d)my = b;
     * 又因为最开始的a * x ≡ b(mod m) 可以变形为 ax = my + b
     * 所以我们要求的x就等于 x * (b / d) % m,
     * 最后模上m是以为防止爆int
     * 相当于 ax % m = b  ==> (a (x % m)) % m
     ***/

    public static void main(String[] args) {
        n = sc.nextInt();
        while (n-- > 0) {
            a = sc.nextInt();
            b = sc.nextInt();
            m = sc.nextInt();
            int d = exgcd(a, m, x, y);
            if (b % d > 0) {
                out.println("impossible");
            } else {
                out.println((long) x[0] * b / d % m);
            }
        }
        out.flush();
    }

    static int exgcd(int a, int b, int[] x, int[] y) {
        if (b == 0) {
            x[0] = 1;
            y[0] = 0;
            return a;
        }
        int d = exgcd(b, a % b, y, x);
        y[0] = y[0] - a / b * x[0];
        return d;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
